package ch.zbw.pra.sysventory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator.FileType;
import ch.zbw.pra.sysventory.domain.visualisation.VisuControll;
import ch.zbw.pra.sysventory.ui.MainFrame;
import ch.zbw.pra.sysventory.ui.gui.login.LoginFrame;

import com.thoughtworks.xstream.XStream;

public class SysventoryApp {
	public static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
	private static MainFrame mainFrame = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DomainController.getInstance();
		String title = "*** SysVentory ***   RC V 0,1";
		LoginFrame loginFrame = new LoginFrame(title);
		if (loginFrame.canLogin()) {
			mainFrame = new MainFrame(title);
		}
	}

	public static void saveBeforeClose() {
		XStream theStream = new XStream();
		File file = new FileGenerator().getFile(FileType.SETTING);
		try {
			FileWriter writer = new FileWriter(file);
			theStream.autodetectAnnotations(true);
			theStream.toXML(VisuControll.getInstance(), writer);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(mainFrame, "Fehler beim Schreiben der SettingsDatei in " + file.getAbsolutePath());
		}

	}

	public static MainFrame getMainFrame() {
		return mainFrame;
	}

}
